<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="phoneData" type="text" maxlength="11" placeholder="请输入手机号" :focus="isFocus"></wInput>
				<wInput v-model="passData" type="password" maxlength="20" placeholder="密码6-16位，必须包含数字和字母"></wInput>
			</view>
			<wButton class="wbutton" text="登 录" :rotate="isRotate" @click="startLogin"></wButton>

			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<u-icon name="weixin-fill" color="#4C4C4C" size="40" :index="1" @click="otherLogin"></u-icon>
				</view>
				<view class="login_icon">
					<u-icon name="qq-fill" color="#4C4C4C" size="40" :index="2" @click="otherLogin"></u-icon>
				</view>
				<view class="login_icon">
					<u-icon name="facebook" color="#4C4C4C" size="40" :index="3" @click="otherLogin"></u-icon>
				</view>
			</view>

			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import {
		generalLogin,
		otherLogin,
		getTrtcUserSig
	} from '../../apis/auth_apis.js'
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData: '', //用户/电话
				passData: '', //密码
				isRotate: false, //是否加载旋转
				isFocus: true, // 是否聚焦
				userInfo: {}, //用户信息
			};
		},
		components: {
			wInput,
			wButton,
		},
		onLoad() {},
		methods: {		
			startLogin(e) {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				//判断是否输入手机号，手机号是否规范
				const reg = /^1[3456789]\d{9}$/;
				if (this.phoneData.length == "" || !reg.test(this.phoneData)) {
					uni.showToast({
						icon: 'none',
						mask: true,
						duration: 1000,
						title: '手机号格式不正确'
					});
					return;
				}
				//判断是否输入密码，密码是否规范，长度为6-16位，包含数字和字母，可以有特殊字符
				const phoneReg = /^(?=.*[0-9])(?=.*[a-zA-Z])(.{6,16})$/;
				if (this.passData.length == "" || this.passData.length < 6 || this.passData.length > 16 || !phoneReg.test(
						this.passData)) {
					uni.showToast({
						icon: 'none',
						mask: true,
						duration: 1000,
						title: '密码格式不正确'
					});
					return;
				}
				this.isRotate = true;
				generalLogin({
					phoneNumber: this.phoneData,
					password: this.passData,
				}).then(res => {
					console.log(res)
					this.isRotate = true;
					if (res.code == 20020) {
						console.log("登录成功")
						uni.setStorageSync('token', res.data.token);
						uni.setStorageSync('userInfo', res.data);
						this.$sqliteUtil.init()
						this.$callUtils.login(res.data.id)
						this.$ws.completeClose()
						this.$ws.init();
						setTimeout(function() {
							this.isRotate = false;
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}, 1000);
					} else {
						uni.showToast({
							icon: 'none',
							duration: 1000,
							mask: true,
							title: res.msg,
						});
						this.isRotate = false;
					}
				}).catch(err => {
					this.isRotate = false;
				})
			},
			otherLogin(e) {
				let typeName;
				if (e === 1) {
					typeName = 'weixin';
				} else if (e === 2) {
					typeName = 'qq';
				} else if (e === 3) {
					typeName = 'facebook';
				}
				uni.login({
					provider: typeName,
					success: (res) => {
						otherLogin({
							type: e,
							code: res.authResult.openid
						}).then(res => {
							console.log(res);
							uni.showLoading({
								mask: true,
								title: '登录中...'
							})
							if (res.code == 20020) {
								uni.setStorageSync('token', res.data.token);
								uni.setStorageSync('userInfo', res.data);
								this.$sqliteUtil.init()
								this.$ws.completeClose()
								this.$ws.init();
								this.$callUtils.login(res.data.id)
								setTimeout(function() {
									uni.hideLoading();
								}, 1000);
								uni.reLaunch({
									url: '/pages/index/index'
								});
							} else {
								uni.getUserInfo({
									provider: typeName,
									success: (infoRes) => {
										console.log(infoRes);
										uni.hideLoading();
										this.userInfo = infoRes.userInfo;
										uni.navigateTo({
											url: '/pages/login/bindPhone?data=' +
												JSON.stringify(this.userInfo) +
												'&type=' + e
										})
									},
									fail: (err) => {
										uni.hideLoading();
										uni.showToast({
											title: '授权失败',
											icon: 'error',
											mask: true,
											duration: 1000
										});
									}
								});
							}
						}).catch(err => {
							console.log(err);
							uni.hideLoading();
							uni.showToast({
								icon: 'none',
								duration: 1000,
								mask: true,
								title: '登录失败',
							});
						})
					},
					fail: (err) => {
						uni.showToast({
							title: '登录失败',
							icon: 'error',
							mask: true,
							duration: 1000
						});
					}
				})
			},
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");

	page {
		padding-bottom: 200rpx;
	}
</style>